import {showMessage} from '../../js/my-plugin'
import {getHttpPath} from '../../js/my-utils'
import {_ANIME_PATH_, _TOTAL_PATH_} from '../../js/my-core'

export const doSearch = (_this, val) => {
  _this.$axios({
    method: 'get',
    url: '/api/search?s=' + val
  })
    .then((res) => {
      if (res.data.code === 200) {
        _this.animePlays = []
        let i = 0
        for (let item of res.data.data) {
          // 传入anime信息
          let anime = {name: '', cover: null, uri: '#'}
          anime.name = item.anime.name
          anime.cover = getHttpPath(item.anime.cover)
          anime.uri = _TOTAL_PATH_[0] + item.anime.id
          anime.info = item.anime.info
          anime.count = item.anime.count
          anime.score = item.anime.score
          anime.startTime = new Date(item.anime.startTime)
          anime.tag = item.anime.tag
          anime.status = item.anime.status
          // 传入animevideo信息
          let list = []
          let j = 0
          for (let ii of item.list) {
            let a = {id: j, no: ii.no, url: _ANIME_PATH_ + ii.id}
            list.push(a)
            j++
          }
          // 总赋值
          const animePlay = {anime: anime, list: list}
          const it = {id: i, animePlay: animePlay}
          _this.animePlays.push(it)
          i++
        }
      } else {
        showMessage(res.data.msg, 0)
      }
    })
    .catch((error) => {
      console.log(error)
      showMessage('登陆失败，无法连接服务器', 0)
    })
}
